﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SQLite;


namespace i1
{
    public partial class Form1 : Form
    {
        public SQLiteConnection Sql;
        private String word_Chinese;
        private String word_English;
        
        public Form1()
        {
            InitializeComponent();
            Sql = open();
            showWord();
        }
        
        public SQLiteConnection open()
        {
            //数据库地址 字符串
            
            string FilePath = @"Data Source= D:\csharpDB\ch_en.db";
            //new 数据库对象
            Sql = new SQLiteConnection(FilePath);
            //打开数据库
            Sql.Open();
            //返回数据库连接对象
            return Sql;
        }

        public bool showWord()
        {
            Random ran = new Random();
            int id = ran.Next(1,11);
            
            try
            {
                String strSql = "SELECT * FROM [words] WHERE word_id="+id;
                DataTable dt = new DataTable();
                //SQLiteCommand command = new SQLiteCommand("select * from GJB420A", dataConn);
                SQLiteCommand cmd = new SQLiteCommand(strSql, Sql);
                //cmd.ExecuteNonQuery();
                //SQLiteDataAdapter mAdapt = new SQLiteDataAdapter(cmd);
                //mAdapt.Fill(dt);
                SQLiteDataReader dr = cmd.ExecuteReader();
                while (dr.Read())//读取
                {
                    this.word_Chinese = Convert.ToString(dr["ch"]);
                    this.word_English = Convert.ToString(dr["en"]);
                    CHN_lb.Text = this.word_Chinese;
                    //Console.Write(Convert.ToString(dr["ch"]));
                }

                return true;                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return false;
            }
        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            if (txt_en.Text == this.word_English)
            {
                result.Text = "正确";
                showWord();
            }
            else
            {
                result.Text = "错误";
            }
        }
    }
}
